package com.etime.perfectschool.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.perfectschool.pojo.SchoolNews;
import com.etime.perfectschool.service.NewsCommentsService;
import com.etime.perfectschool.service.SchoolNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class SchoolNewsController {
    @Autowired
    private SchoolNewsService schoolNewsService;


    //查询全部新闻
    @GetMapping("/getAllSchoolNews")
    public Page<SchoolNews> getAllSchoolNews(int current, int size) {
        return schoolNewsService.getAllSchoolNews(current,size);
    }
    //发布新闻
    @PostMapping("/addSchoolNews")
    public int addSchoolNews(SchoolNews schoolNews)
    {

        int newsId = schoolNewsService.addSchoolNews(schoolNews);
        return newsId;
    }

    //删除新闻b
    @DeleteMapping("/deleteSchoolNews")
    public boolean deleteSchoolNews(int newsId)
    {
        return schoolNewsService.deleteSchoolNews(newsId);
    }
    //点赞功能的实现
    @PutMapping("/likeSchoolNews")
    public SchoolNews likeSchoolNews(int newsId)
    {
        int count=0;
        SchoolNews schoolNews = schoolNewsService.getSchoolNews(newsId);
        count=schoolNews.getNewsUpvote();
        if(count==0)
        {
            schoolNews.setNewsUpvote(1);
        }else {
          count=count+1;
          schoolNews.setNewsUpvote(count);
        }
        boolean res = schoolNewsService.likeSchoolNews(schoolNews);
        if(res)
        {
             schoolNews = schoolNewsService.getSchoolNews(newsId);
        }
        return schoolNews;
    }
    //查询某条新闻
    @GetMapping("/getSchoolNews")
    public SchoolNews getSchoolNews(int newsId)
    {
        return schoolNewsService.getSchoolNews(newsId);
    }

    //修改新闻
    @PutMapping("/updateSchoolNews")
    public boolean updateSchoolNews(SchoolNews schoolNews)
    {

        return schoolNewsService.updateSchoolNews(schoolNews);
    }



}
